Strict

Import agfx

Global myTmpScreen:TTmpScreen

Global gameScreen:TGameScreen
Global titleScreen:TTitleScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		'DEVICE.SetFps(30)		' default FPS is 60
		
		Super.OnCreate()
		
		Local tmpImage:Image

		IMAGES.LoadAnim("Ship1.png", 64, 64, 7, null)

		
		gameScreen = New TGameScreen
		titleScreen = New TTitleScreen
		
		titleScreen.Prepare()
		
		Return 0
	End
	
	#Rem OPTIONAL 
	
	Method OnUpdate:Int()
		Super.OnUpdate()
	End
	
	Method OnRender:Int()
		Super.OnRender()
			
	End
	
	Method OnResume:Int()
	
	End
	
	Method OnSuspend:Int()
	
	End
	
	Method OnPause:Int()
	
	End
	
	Method OnLoading:Int()
	
	End
	
	#End
	
End


Class TTitleScreen Extends TScreen

	Method New()
		name = "Title"
	End

	Method Start:Void()
		APP.fadeScreen.Start(50, False)
	End
	
	Method Render:Void()
		Cls
		DrawText "TITLE SCREEN!", DISPLAY.VSCREEN_HALF_WIDTH , DISPLAY.VSCREEN_HALF_HEIGHT , 0.5, 0.5
		DrawText "Click to Play!", DISPLAY.VSCREEN_HALF_WIDTH , DISPLAY.VSCREEN_HALF_HEIGHT + 20, 0.5, 0.5
	End

	Method Update:Void()
		If KeyHit(KEY_SPACE) Or MOUSE.Hit(0)
			APP.fadeScreen.Start(50, True)
			APP.nextScreen = gameScreen
		End
		If KeyHit(KEY_ESCAPE)
			APP.fadeScreen.Start(50, True)
			APP.nextScreen = APP.exitScreen
		End
	End
End

Class TGameScreen Extends TScreen
	Field player:Player

	Method New()
		name = "Game"
	End
	
	Method Start:Void()
		APP.fadeScreen.Start(50, False)
		player = New Player(IMAGES.Find("Ship1"), DISPLAY.VSCREEN_HALF_WIDTH , DISPLAY.VSCREEN_HALF_HEIGHT )
	End
	
	Method Render:Void()
		Cls
		player.Draw()
		DrawHUD()
	End

	Method Update:Void()
	
		player.UpdateAnimationEvent()
		player.Update()
		
		If KeyHit(KEY_1)
			player.SetAnimationEvent(0, 6, 100, False, False)
		End
		If KeyHit(KEY_2)
			player.SetAnimationEvent(0, 6, 100, False, True)			
		End
		If KeyHit(KEY_3)
			player.SetAnimationEvent(0, 6, 100, True, False)			
		End
		If KeyHit(KEY_4)
			player.SetAnimationEvent(0, 6, 100, True, True)			
		End
		
		If KeyHit(KEY_Q)
			player.SetAnimationEvent(6, 0, 100, False, False)
		End
		If KeyHit(KEY_W)
			player.SetAnimationEvent(6, 0, 100, False, True)			
		End
		If KeyHit(KEY_E)
			player.SetAnimationEvent(6, 0, 100, True, False)			
		End
		If KeyHit(KEY_R)
			player.SetAnimationEvent(6, 0, 100, True, True)			
		End
		
		If KeyHit(KEY_ESCAPE)
			APP.fadeScreen.Start(50, True)
			APP.nextScreen = titleScreen
		End
	End
		
	Method DrawHUD:Void()
		DrawText("frame = "+player.GetFrame(), 10, 0)
	
		If player.pingPong
			DrawText("pingPong = true" , 10, 30)
		Else
			DrawText("pingPong = false" , 10, 30)
		End
		If player.loop
			DrawText("loop = true" , 10, 60)
		Else
			DrawText("loop = false" , 10, 60)
		End	
		'DrawText("startFrame = "+player.frameStart, 10, 90)
		'DrawText("startEnd = "+player.frameEnd, 10, 120)
		
		DrawText("Press 1 - 4 to change animation", 10, 150)

		DrawText("Press <ESC> to return to the Title Screen", 10, DISPLAY.VSCREEN_HEIGHT-12)

		
		FPS.Draw(DISPLAY.VSCREEN_WIDTH, DISPLAY.VSCREEN_HEIGHT  - 12, 1)
		
	End
End


Class Player Extends TSprite
    Field vx:Float, vy:Float
    Field ax:Float, ay:Float
    Field direction:Float
	
	Method New(gi:TImage, x#, y#)
		Super.New(gi, x, y)
		direction = -90
		SetFrame(3)
	End
	
	Method Update:Void()
		Local Acceleration:Float
		
		If KeyDown(KEY_UP)
			Acceleration=.2
		Else If KeyDown(KEY_DOWN)
			Acceleration=-.2
		Else
			Acceleration=0
		End If		
		
		If KeyDown(KEY_LEFT)
			direction-=2
			Self.rotation+=2
		End If
		
		If KeyDown(KEY_RIGHT)
			direction+=2
			Self.rotation-=2
		End If
		
		Local ax:Float = Acceleration * Cos(direction)
		Local ay:Float = Acceleration * Sin(direction)
		
		vx+=ax 
		vy+=ay
		
		
		Self.position.x+=vx * TIMER.delta
		Self.position.y+=vy * TIMER.delta
		
		If (Self.position.x < 0) Then 
			Self.position.x = DISPLAY.VSCREEN_WIDTH
		EndIf
		
		If (Self.position.x > DISPLAY.VSCREEN_WIDTH) Then
			Self.position.x = 0
		EndIf
		
		If (Self.position.y < 0) Then
			Self.position.y = DISPLAY.VSCREEN_HEIGHT
		EndIf
		
		If (Self.position.y > DISPLAY.VSCREEN_HEIGHT) Then
			Self.position.y = 0
		EndIf
	End
	
End




